﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Swin_e_mon_Forms
{
    public partial class SwinEForm : Form
    {
        private CommandProcessor cp;
        private Player p;
        private Color userColor = Color.Black, cmdColor = Color.Red;
        private Form form;

        public SwinEForm()
        {
            IWorldLoader loader = new TextFileWorldLoader("data.txt");
            Icon = new Icon(Application.StartupPath + @"\icon.ico");
            cp = new CommandProcessor();
            p = new Player();
            Location start = loader.Load();
            p.InLocation = start;
            cp.AddCommand(new LookCommand(this));
            cp.AddCommand(new TransferCommand(this));
            cp.AddCommand(new MoveCommand(this));
            InitializeComponent();
            outputBox.SelectedText = "Welcome to Swin - e - Mon!";
        }

        public void ClearLocList()
        {
            LocationInv.Items.Clear();
        }

        public void ClearPlayerList()
        {
            PlayerInv.Items.Clear();
        }

        public void RemoveFromLocList(string iName)
        {
            LocationInv.Items.Remove(iName);
        }

        public void RemoveFromPlayerList(string iName)
        {
            PlayerInv.Items.Remove(iName);
        }

        public void AddToLocList(string iName)
        {
            LocationInv.Items.Add(iName);
        }

        public void AddToPlayerList(string iName)
        {
            PlayerInv.Items.Add(iName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            processText(inputBox.Text);
        }

        private void inputBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                processText(inputBox.Text);
        }

        private void redToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Red;
        }

        private void greenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Green;
        }

        private void blueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Black;
        }

        private void orangeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Orange;
        }

        private void blackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Black;
        }

        private void purpleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Purple;
        }

        private void pinkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userColor = Color.Pink;
        }

        private void redToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Red;
        }

        private void greenToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Green;
        }

        private void blueToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Blue;
        }

        private void orangeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Orange;
        }

        private void purpleToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Purple;
        }

        private void blackToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Black;
        }

        private void pinkToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cmdColor = Color.Pink;
        }

        private void aboutPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This version of Swin e Mon was designed by the badass Andrew Cain and coded by David Desouza.");
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IWorldLoader loader = new TextFileWorldLoader("data.txt");
            Location start = loader.Load();
            p.InLocation = start;
            outputBox.Clear();
        }

        private void saveLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveLog.Filter = "Text Files|*.txt|All Files|*.*";

            SaveLog.ShowDialog();

            outputBox.SaveFile(SaveLog.FileName, RichTextBoxStreamType.PlainText);
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void processText(string id)
        {
            changeColor(id);
            outputBox.SelectionStart = outputBox.Text.Length;
            outputBox.ScrollToCaret();
            outputBox.Refresh();
            inputBox.Clear();
        }

        private void changeColor(string id)
        {
            outputBox.SelectionColor = userColor;
            outputBox.SelectedText = id + "\r\n";
            outputBox.SelectionColor = cmdColor;
            outputBox.SelectedText = cp.Run(p, id) + "\r\n";
        }


        private void findID(string command, string[] ids)
        {
            
            ids = ids[1].Split(')');
            processText(command + " " + ids[0]);
        }

        private void pickUp(object sender, EventArgs e)
        {
            if (LocationInv.SelectedItem == null) return;
            string[] ids = LocationInv.SelectedItem.ToString().Split('(');
            findID("PickUp", ids);
        }

        private void drop(object sender, EventArgs e)
        {
            if (PlayerInv.SelectedItem == null) return;
            string[] ids = PlayerInv.SelectedItem.ToString().Split('(');
            findID("Drop",ids);
        }

        private void look(object sender, EventArgs e)
        {
            processText("Look");
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void outputBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            form = new Creater(cp,p,this);
            form.ShowDialog();
        }


    }
}
